home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / board / GNUChess4_0_58.lha / gnuchess-4.0 / src / new / main.c < prev    next >
C/C++ Source or Header  |  1992-08-26  |  9KB  |  393 lines

  1. /*
  2.  * main.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1988,1989,1990 John Stanback
  5.  * Copyright (c) 1992 Free Software Foundation
  6.  *
  7.  * This file is part of GNU CHESS.
  8.  *
  9.  * GNU Chess is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2, or (at your option)
  12.  * any later version.
  13.  *
  14.  * GNU Chess is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with GNU Chess; see the file COPYING.  If not, write to
  21.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #include "version.h"
  25. #include "gnuchess.h"
  26. #include <signal.h>
  27. char *ColorStr[2];
  28. char *CP[CPSIZE];
  29. /*
  30.  * In a networked enviroment gnuchess might be compiled on different hosts
  31.  * with different random number generators, that is not acceptable if they
  32.  * are going to share the same transposition table.
  33.  */
  34. unsigned long int next = 1;
  35.  
  36. unsigned int
  37. urand (void)
  38. {
  39.   next *= 1103515245;
  40.   next += 12345;
  41.   return ((unsigned int) (next >> 16) & 0xFFFF);
  42. }
  43.  
  44. void
  45. srand (unsigned int seed)
  46. {
  47.   next = seed;
  48. }
  49.  
  50. unsigned long hashkey, hashbd;
  51. struct hashval hashcode[2][7][64];
  52.  
  53. #ifdef ttblsz
  54. struct hashentry huge ttable[2][vttblsz + MAXrehash];
  55. unsigned int ttblsize;
  56. #endif
  57.  
  58.  
  59. char savefile[128] = "";
  60. char listfile[128] = "";
  61. #ifdef HISTORY
  62. unsigned char history[32768];
  63. #endif
  64. short rpthash[2][256];
  65. struct leaf Tree[TREE], *root;
  66. short TrPnt[MAXDEPTH];
  67. short PieceList[2][64], PawnCnt[2][8];
  68. short castld[2], Mvboard[64];
  69. short svalue[64];
  70. struct flags flag;
  71. short opponent, computer, WAwindow, WBwindow, BAwindow, BBwindow, dither, INCscore;
  72. long ResponseTime, ExtraTime, MaxResponseTime, et, et0, time0, ft;
  73. long NodeCnt, ETnodes, EvalNodes, HashCnt, HashAdd, FHashCnt, FHashAdd, HashCol,
  74.  THashCol, filesz;
  75. long replus, reminus;
  76. short HashDepth = HASHDEPTH, HashMoveLimit = HASHMOVELIMIT;
  77. short player, xwndw, rehash;
  78. struct GameRec GameList[MAXMOVES + MAXDEPTH];
  79. short Sdepth, GameCnt, Game50, MaxSearchDepth;
  80. short epsquare, contempt;
  81. int Book;
  82. struct TimeControlRec TimeControl;
  83. short TCflag, TCmoves, TCminutes, TCseconds, OperatorTime;
  84. short XCmoves[3], XCminutes[3], XCseconds[3], XC, XCmore;
  85. const short otherside[3] =
  86. {black, white, neutral};
  87. unsigned short hint;
  88. short int TOflag;        /* force search re-init if we backup search */
  89.  
  90. short mtl[2], pmtl[2], hung[2];
  91. short Pindex[64];
  92. short PieceCnt[2];
  93. short FROMsquare, TOsquare;
  94. short HasKnight[2], HasBishop[2], HasRook[2], HasQueen[2];
  95. short ChkFlag[MAXDEPTH], CptrFlag[MAXDEPTH], PawnThreat[MAXDEPTH];
  96. short Pscore[MAXDEPTH], Tscore[MAXDEPTH];
  97. const short qrook[3] =
  98. {0, 56, 0};
  99. const short krook[3] =
  100. {7, 63, 0};
  101. const short kingP[3] =
  102. {4, 60, 0};
  103. const short rank7[3] =
  104. {6, 1, 0};
  105. const short sweep[8] =
  106. {false, false, false, true, true, true, false, false};
  107. unsigned short killr0[MAXDEPTH], killr1[MAXDEPTH];
  108. unsigned short killr2[MAXDEPTH], killr3[MAXDEPTH];
  109. unsigned short PV, SwagHt, Swag0, Swag1, Swag2, Swag3, Swag4, sidebit;
  110. #ifdef KILLT
  111. short killt[0x4000];
  112. #endif
  113. const short value[7] =
  114. {0, valueP, valueN, valueB, valueR, valueQ, valueK};
  115. const short control[7] =
  116. {0, ctlP, ctlN, ctlB, ctlR, ctlQ, ctlK};
  117. short stage, stage2, Developed[2];
  118. FILE *hashfile;
  119. unsigned int starttime;
  120. short int ahead = true, hash = true;
  121.  
  122. #if defined CHESSTOOL || defined XBOARD
  123. void
  124. TerminateChess (int sig)
  125. {
  126.   ExitChess();
  127. }
  128. #endif
  129.  
  130. /* hmm.... shouldn`t main be moved to the interface routines */
  131. int
  132. main (int argc, char **argv)
  133. {
  134.   char *xwin = 0;
  135.   char *Lang = NULL;
  136.   srand (starttime = ((unsigned int) time ((long *) 0)));    /* init urand */
  137. #ifdef ttblsz
  138.   ttblsize = ttblsz;
  139.   rehash = -1;
  140. #endif /* ttblsz */
  141.   if (argc > 2)
  142.     {
  143.       if (argv[1][0] == '-' && argv[1][1] == 'L')
  144.     {
  145.       Lang = argv[2];
  146.       argv += 2;
  147.       argc -= 2;
  148.     }
  149.     }
  150.   InitConst (Lang);
  151.   ColorStr[0] = CP[118];
  152.   ColorStr[1] = CP[119];
  153.  
  154.   while (argc > 1 && ((argv[1][0] == '-') || (argv[1][0] == '+')))
  155.     {
  156.       switch (argv[1][1])
  157.     {
  158.     case 'a':
  159.       ahead = ((argv[1][0] == '-') ? false : true);
  160.       break;
  161.     case 'h':
  162.       hash = ((argv[1][0] == '-') ? false : true);
  163.       break;
  164.     case 's':
  165.       argc--;
  166.       argv++;
  167.       if (argc > 1)
  168.         strcpy (savefile, argv[1]);
  169.       break;
  170.     case 'l':
  171.       argc--;
  172.       argv++;
  173.       if (argc > 1)
  174.         strcpy (listfile, argv[1]);
  175.       break;
  176.  
  177. #if ttblsz
  178.     case 'r':
  179.       if (argc > 2)
  180.         rehash = atoi (argv[2]);
  181.       argc--;
  182.       argv++;
  183.       if (rehash > MAXrehash)
  184.         rehash = MAXrehash;
  185.       break;
  186.     case 'T':
  187.       if (argc > 2)
  188.         ttblsize = atoi (argv[2]);
  189.       argc--;
  190.       argv++;
  191.       if (ttblsize > 0 && ttblsize < 24)
  192.         ttblsize = (1 << ttblsize);
  193.       else
  194.         ttblsize = ttblsz;
  195.       break;
  196. #ifdef HASHFILE
  197.     case 't':        /* create or test persistent transposition
  198.                  * table */
  199.       hashfile = fopen (HASHFILE, RWA_ACC);
  200.       if (hashfile)
  201.         {
  202.           fseek (hashfile, 0L, SEEK_END);
  203.           filesz = (ftell (hashfile) / sizeof (struct fileentry)) - 1;
  204.         }
  205.       if (hashfile != NULL)
  206.         {
  207.           long i, j;
  208.           int nr[MAXDEPTH];
  209.           struct fileentry n;
  210.  
  211.           printf (CP[49]);
  212.           for (i = 0; i < MAXDEPTH; i++)
  213.         nr[i] = 0;
  214.           fseek (hashfile, 0L, SEEK_END);
  215.           i = ftell (hashfile) / sizeof (struct fileentry);
  216.           fseek (hashfile, 0L, SEEK_SET);
  217.           for (j = 0; j < i + 1; j++)
  218.         {
  219.           fread (&n, sizeof (struct fileentry), 1, hashfile);
  220.           if (n.depth)
  221.             {
  222.               nr[n.depth]++;
  223.               nr[0]++;
  224.             }
  225.         }
  226.           printf (CP[109],
  227.               nr[0], i);
  228.           for (j = 1; j < MAXDEPTH; j++)
  229.         printf ("%d ", nr[j]);
  230.           printf ("\n");
  231.         }
  232.       return 0;
  233.     case 'c':        /* create or test persistent transposition
  234.                  * table */
  235.       if (argc > 2)
  236.         filesz = atoi (argv[2]);
  237.       if (filesz > 0 && filesz < 24)
  238.         filesz = (1 << filesz) - 1 + MAXrehash;
  239.       else
  240.         filesz = Deffilesz + MAXrehash;
  241.       if ((hashfile = fopen (HASHFILE, RWA_ACC)) == NULL)
  242.         hashfile = fopen (HASHFILE, WA_ACC);
  243.       if (hashfile != NULL)
  244.         {
  245.           long j;
  246.           struct fileentry n;
  247.  
  248.           printf (CP[66]);
  249.           for (j = 0; j < 32; j++)
  250.         n.bd[j] = 0;
  251.           n.f = n.t = 0;
  252.           n.flags = 0;
  253.           n.depth = 0;
  254.           n.sh = n.sl = 0;
  255.           for (j = 0; j < filesz + 1; j++)
  256.         fwrite (&n, sizeof (struct fileentry), 1, hashfile);
  257.           fclose (hashfile);
  258.         }
  259.       else
  260.         printf (CP[50], HASHFILE);
  261.       return (0);
  262. #endif /* HASHFILE */
  263. #endif /* ttblsz */
  264.     case 'x':
  265.       xwin = &argv[1][2];
  266.       break;
  267.     case 'v':
  268.       fprintf (stderr, CP[102], version, patchlevel);
  269.       exit (1);
  270.     default:
  271.       fprintf (stderr, CP[113]);
  272.       exit (1);
  273.     }
  274.       argv++;
  275.       argc--;
  276.     }
  277.   XC = 0;
  278.   MaxResponseTime = 0;
  279. #if defined CHESSTOOL || defined XBOARD
  280.   signal (SIGTERM, TerminateChess);
  281.   TCflag = true;
  282.   TCmoves = 40;
  283.   TCminutes = 120;
  284.   TCseconds = 0;
  285.   OperatorTime = 0;
  286. #else
  287.   TCflag = false;
  288.   OperatorTime = 0;
  289. #endif
  290.   if (argc == 2)
  291.     {
  292.       char *p;
  293.  
  294.       MaxResponseTime = 100L*strtol(argv[1], &p, 10);
  295.       if (*p == ':')
  296.     MaxResponseTime = 60L*MaxResponseTime + 
  297.         100L*strtol(++p, (char **) NULL, 10);
  298.       TCflag = false;
  299.       TCmoves = 0;
  300.       TCminutes = 0;
  301.       TCseconds = 0;
  302.     }
  303.   if (argc >= 3)
  304.     {
  305.       char *p;
  306.       if (argc > 9)
  307.     {
  308.       printf ("%s\n", CP[220]);
  309.       exit (1);
  310.     }
  311.       TCmoves = atoi (argv[1]);
  312.       TCminutes = strtol (argv[2], &p, 10);
  313.       if (*p == ':')
  314.     TCseconds = strtol (p + 1, (char **) NULL, 10);
  315.       else
  316.     TCseconds = 0;
  317.       TCflag = true;
  318.       argc -= 3;
  319.       argv += 3;
  320.       while (argc > 1)
  321.     {
  322.       XCmoves[XC] = atoi (argv[0]);
  323.       XCminutes[XC] = strtol (argv[1], &p, 10);
  324.       if (*p == ':')
  325.         XCseconds[XC] = strtol (p + 1, (char **) NULL, 10);
  326.       else
  327.         XCseconds[XC] = 0;
  328.       if (XCmoves[XC] && (XCminutes[XC] || XCseconds[XC]))
  329.         XC++;
  330.       else
  331.         {
  332.           printf (CP[220]);
  333.           exit (1);
  334.         }
  335.       argc -= 2;
  336.       argv += 2;
  337.     }
  338.       if (argc)
  339.     {
  340.       printf ("%s\n", CP[220]);
  341.       exit (1);
  342.     }
  343.     }
  344.   Initialize ();
  345. #ifdef ttblsz
  346.   Initialize_ttable ();
  347. #endif /* ttblsz */
  348.   Initialize_dist ();
  349.   Initialize_moves ();
  350.   NewGame ();
  351.  
  352.   flag.easy = ahead;
  353.   flag.hash = hash;
  354.   if (xwin)
  355.     xwndw = atoi (xwin);
  356.  
  357.   hashfile = NULL;
  358. #if ttblsz
  359. #ifdef HASHFILE
  360.   hashfile = fopen (HASHFILE, RWA_ACC);
  361.   if (hashfile)
  362.     {
  363.       fseek (hashfile, 0L, SEEK_END);
  364.       filesz = ftell (hashfile) / sizeof (struct fileentry) - 1;
  365.     }
  366. #if !defined CHESSTOOL && !defined XBOARD
  367.   else
  368.     ShowMessage (CP[98]);
  369. #endif
  370. #endif /* HASHFILE */
  371. #endif /* ttblsz */
  372.   while (!(flag.quit))
  373.     {
  374.       if (flag.bothsides && !flag.mate)
  375.     SelectMove (opponent, 1);
  376.       else
  377.     InputCommand ();
  378.       if (!(flag.quit || flag.mate || flag.force))
  379.     {
  380.       SelectMove (computer, 1);
  381.     }
  382.     }
  383. #if ttblsz
  384. #ifdef HASHFILE
  385.   if (hashfile)
  386.     fclose (hashfile);
  387. #endif /* HASHFILE */
  388. #endif /* ttblsz */
  389.  
  390.   ExitChess ();
  391.   return (0);
  392. }
  393.